<?php

namespace Home\Model;
use Think\Model;

class ModelApiModel extends Model {
	
	//获取列表
	public function getList($status = -1,$page = 1,$pagesize = 20){
		if($status){
			$where['api_status'] = array('egt',0);
		}else{
			$where['api_status'] = $status;
		}
		
		$count = $this->where($where)->count();
		$list = $this->where($where)->limit( ($page-1)*$pagesize,$pagesize )->select();

		$data['count'] = $count;
		$data['list'] = $list;
		return $data;
	}

	//获取单条记录
	public function getInfo($id = null){
		if(empty($id)) return null;

		$where['id'] = $id;
		$result = $this->where($where)->find();
		return $result;
	}

	//更新单条记录
	public function updateOne($id,$data){
		if(empty($id) || empty($data)) return false;

		$where['id'] = $id;
		$result = $this->where($where)->save($data);
		return $result;
	}

	//删除单条记录
	public function deleteOne($id = null){

	}

	//获取用户导入的数据是否存在
	public function getImportList($uid){
		if(empty($uid)) return null;

		//获取导入“db”数据库的数据
		$db_sql = "select id,name,1 as type from dmp_data_db where uid=".$uid;
		$db_result = $this->query($db_sql);

		//获取导入“文件”数据库的数据
		$file_sql = "select id,name,2 as type from dmp_data_file where uid=".$uid;
		$file_result = $this->query($file_sql);

		$data = array_merge($db_result,$file_result);
		return $data;
	}

	//检查用户是否有导入数据
	public function isexistsImport($uid){
		if(empty($uid)) return false;

		//检查“db”库数量
		$db_count_sql = "select count(*) from dmp_data_db where uid=".$uid;
		$db_count_result = $this->query($db_count_sql);
		if(empty($db_count_result)){
			$file_count_sql = "select count(*) from dmp_data_file where uid=".$uid;
			$file_count_result = $this->query($file_count_sql);
			if(empty($file_count_result)){
				return false;
			}else{
				return true;
			}
		}else{
			return true;
		}

	}

	//获取导入数据的信息
	public function getImportInfo($id = null,$type=1){
		if(empty($id)) return null;

		switch($type){
			case 1:
				$sql = "select * from dmp_data_db where id=".(int) $id;
				break;
			case 2:
				$sql = "select * from dmp_data_file where id=".(int)$id;
				break;
		}

		$info = $this->query($sql);
		if(!empty($info)){
			return $info[0];
		}else{
			return null;
		}

	}

}
?>